//
//  BookCell.m
//  InstrumentsTutorial
//
//  Created by Matt Galloway on 05/08/2012.
//  Copyright (c) 2012 Swipe Stack Ltd. All rights reserved.
//

#import "PhotoCell.h"

#import "FlickrPhoto.h"
#import "ImageCache.h"

@interface PhotoCell ()

@property (nonatomic, strong) NSArray *urls;

@end

@implementation PhotoCell

- (NSArray *)urls {
    if (!_urls) {
        _urls = @[@"http://pic2.ooopic.com/01/03/51/25b1OOOPIC19.jpg",
                  @"http://baike.soso.com/p/20090711/20090711101754-314944703.jpg",
                  @"http://img2.3lian.com/img2007/19/33/005.jpg",
                  @"http://img.taopic.com/uploads/allimg/130501/240451-13050106450911.jpg",
                  @"http://pica.nipic.com/2008-03-19/2008319183523380_2.jpg",
                  @"http://imgsrc.baidu.com/forum/pic/item/3ac79f3df8dcd1004e9102b8728b4710b9122f1e.jpg",
                  @"http://pic.nipic.com/2007-11-09/200711912230489_2.jpg",
                  @"http://img2.3lian.com/img2007/10/28/123.jpg",
                  @"http://pic1.nipic.com/2008-08-12/200881211331729_2.jpg",
                  @"http://pic9.nipic.com/20100812/3289547_144304019987_2.jpg",
                  @"http://pic13.nipic.com/20110415/1347158_132411659346_2.jpg",
                  @"http://a2.att.hudong.com/38/59/300001054794129041591416974.jpg",
                  @"http://pica.nipic.com/2008-01-09/200819134250665_2.jpg",
                  @"http://ppt360.com/background/UploadFiles_6733/201012/2010122016291897.jpg",
                  @"http://pic27.nipic.com/20130126/9252150_172332132344_2.jpg",
                  @"http://pic.nipic.com/2007-11-08/2007118192311804_2.jpg",
                  @"http://img3.imgtn.bdimg.com/it/u=2186049766,4202562417&fm=21&gp=0.jpg",
                  @"http://img.taopic.com/uploads/allimg/120403/57997-12040319145195.jpg",
                  @"http://www.photophoto.cn/m55/024/037/0240370130.jpg",
                  @"http://pica.nipic.com/2007-12-16/20071216105113620_2.jpg",
                  @"http://img1.3lian.com/img2008/06/019/ych.jpg",
                  @"http://pic.nipic.com/2007-11-08/200711819133664_2.jpg",
                  @"http://pic1.nipic.com/2009-02-19/200921922311483_2.jpg",
                  @"http://pic.nipic.com/2008-04-30/2008430112252842_2.jpg",
                  @"http://pic26.nipic.com/20130114/9252150_140310235330_2.jpg",
                  ];
    }
    return _urls;
}

- (void)setPhoto:(FlickrPhoto *)photo {
    _photo = photo;
    
    self.textLabel.text = photo.title;
    
    NSInteger idx = arc4random()%self.urls.count;
    NSLog(@"idx = %zd", idx);
    _photo.thumbnailUrl = [NSURL URLWithString:self.urls[idx]];

    //TODO:修改点，此处有内存泄露
    NSData *imageData = [NSData dataWithContentsOfURL:_photo.thumbnailUrl];
    self.imageView.image = [UIImage imageWithData:imageData];
    
//    [[ImageCache sharedInstance] downloadImageAtURL:_photo.thumbnailUrl
//                                  completionHandler:^(UIImage *image) {
//                                      self.imageView.image = image;
//                                      [self setNeedsLayout];
//                                  }];
    
}

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])) {
    }
    return self;
}

@end
